package com.xuebusi.toutiao.admin.api.common.exception;

import com.xuebusi.toutiao.admin.api.common.result.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理
 */
@Slf4j
@RestControllerAdvice
public class ApiExceptionHandler {
    /**
     * 自定义异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public ResponseEntity<ApiResult> handlerCustomException(CustomException e) {
        log.error(">>> {}", e.getMessage());
        HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        try {
            httpStatus = HttpStatus.valueOf(e.getCode());
        } catch (Exception ex) {
            log.error(">>> {}", ex.getMessage());
        }
        ApiResult apiResult = ApiResult.error(e.getCode(), e.getMessage());
        ResponseEntity responseEntity = new ResponseEntity(apiResult, httpStatus);
        return responseEntity;
    }

    /**
     * 参数校验异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BindException.class)
    public ResponseEntity<ApiResult> handlerBindException(BindException e) {
        FieldError fieldError = e.getBindingResult().getFieldError();
        String field = fieldError.getField();
        String defaultMessage = fieldError.getDefaultMessage();
        log.error(">>> {}[{}]", defaultMessage, field);
        ApiResult apiResult = new ApiResult(HttpStatus.BAD_REQUEST.value(), defaultMessage, null);
        return ResponseEntity.badRequest().body(apiResult);
    }

    /**
     * 不支持的请求方法异常
     *
     * @param e 异常对象
     * @return
     */
    @ExceptionHandler
    public ResponseEntity handlerMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        HttpStatus httpStatus = HttpStatus.METHOD_NOT_ALLOWED;
        ApiResult apiResult = new ApiResult(httpStatus.value(), e.getMessage(), null);
        return new ResponseEntity(apiResult, httpStatus);
    }

    /**
     * 缺少请求参数异常
     *
     * @param e 异常对象
     * @return
     */
    @ExceptionHandler
    public ResponseEntity handlerMethodNotSupportedException(MissingServletRequestParameterException e) {
        HttpStatus httpStatus = HttpStatus.BAD_REQUEST;
        ApiResult apiResult = new ApiResult(httpStatus.value(), e.getMessage(), null);
        return new ResponseEntity(apiResult, httpStatus);
    }

    /**
     * 其他异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ApiResult> handlerException(Exception e) {
        log.error(">>> {}", e.getMessage());
        HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        ApiResult apiResult = new ApiResult(httpStatus.value(), e.getMessage(), null);
        return new ResponseEntity<>(apiResult, httpStatus);
    }
}
